from mindspore import Tensor
from mindspore import numpy as np
from mindspore.common.initializer import One
import mindspore,os
import h5py as h5
def demo01():
    t1 = Tensor(shape=(2,2),dtype=mindspore.int32,init=One())
    t2 = Tensor(np.array([[1,2,5],[3,4,2]]),mindspore.int32)
    print(f"t1={t1}\nt2={t2}",end='\n')
    print(t2.argmax(1))
#张量序列化
def demo02():
    if os.path.exists('./data'):
        pass
    else:
        os.mkdir('./data')
    #创建张量
    t1 = Tensor(np.reshape(np.arange(16),(4,4)))
    #开始序列化
    filepath = './data/tensor.hdf5'
    f = h5.File(filepath,'w')
    dset = f.create_dataset('matrix',data=t1.asnumpy())
    print(dset)
    f.close()
#反序列化
def demo03():
    filepath = './data/tensor.hdf5'
    f = h5.File(filepath,'r')
    d = f['matrix']
    d1 = d[:]
    print(type(d1))
    t = Tensor(d1)
    print(t)
    f.close()

if __name__ == '__main__':
    demo01()